<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Vue 计算属性和监听器 | Guru Note</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="stylesheet" href="./font/index.css">
    <script src="/docs/icon/svg.js"></script>
    <meta name="description" content="Sea of dawn">
    
    <link rel="preload" href="/docs/assets/css/0.styles.efa081cd.css" as="style"><link rel="preload" href="/docs/assets/js/app.cdec4db9.js" as="script"><link rel="preload" href="/docs/assets/js/2.993bd611.js" as="script"><link rel="preload" href="/docs/assets/js/14.66270d4f.js" as="script"><link rel="preload" href="/docs/assets/js/7.5a12abef.js" as="script"><link rel="prefetch" href="/docs/assets/js/10.c3177bdb.js"><link rel="prefetch" href="/docs/assets/js/100.60c23478.js"><link rel="prefetch" href="/docs/assets/js/101.7ea29e1b.js"><link rel="prefetch" href="/docs/assets/js/102.7f47e485.js"><link rel="prefetch" href="/docs/assets/js/103.ea9ce400.js"><link rel="prefetch" href="/docs/assets/js/104.4ec6a518.js"><link rel="prefetch" href="/docs/assets/js/105.75526347.js"><link rel="prefetch" href="/docs/assets/js/106.01f6b03b.js"><link rel="prefetch" href="/docs/assets/js/107.69fe0811.js"><link rel="prefetch" href="/docs/assets/js/108.fa23768b.js"><link rel="prefetch" href="/docs/assets/js/109.90df1698.js"><link rel="prefetch" href="/docs/assets/js/11.54347528.js"><link rel="prefetch" href="/docs/assets/js/110.dd8d7227.js"><link rel="prefetch" href="/docs/assets/js/111.ccf25ceb.js"><link rel="prefetch" href="/docs/assets/js/112.6963298f.js"><link rel="prefetch" href="/docs/assets/js/113.30ceb3d8.js"><link rel="prefetch" href="/docs/assets/js/114.42ef6603.js"><link rel="prefetch" href="/docs/assets/js/115.f1db7817.js"><link rel="prefetch" href="/docs/assets/js/116.92971223.js"><link rel="prefetch" href="/docs/assets/js/117.c5a65e7e.js"><link rel="prefetch" href="/docs/assets/js/118.e329035d.js"><link rel="prefetch" href="/docs/assets/js/119.cbb17c5e.js"><link rel="prefetch" href="/docs/assets/js/12.c8144ee8.js"><link rel="prefetch" href="/docs/assets/js/120.8537f6a6.js"><link rel="prefetch" href="/docs/assets/js/121.257d3851.js"><link rel="prefetch" href="/docs/assets/js/122.96a5f921.js"><link rel="prefetch" href="/docs/assets/js/123.2220fd12.js"><link rel="prefetch" href="/docs/assets/js/124.552b1a29.js"><link rel="prefetch" href="/docs/assets/js/125.4e36fe37.js"><link rel="prefetch" href="/docs/assets/js/126.3939cdcc.js"><link rel="prefetch" href="/docs/assets/js/127.014df434.js"><link rel="prefetch" href="/docs/assets/js/13.edc237a8.js"><link rel="prefetch" href="/docs/assets/js/15.0804164b.js"><link rel="prefetch" href="/docs/assets/js/16.dec928ab.js"><link rel="prefetch" href="/docs/assets/js/17.bbfc90a8.js"><link rel="prefetch" href="/docs/assets/js/18.76904860.js"><link rel="prefetch" href="/docs/assets/js/19.80a98011.js"><link rel="prefetch" href="/docs/assets/js/20.c39e42bd.js"><link rel="prefetch" href="/docs/assets/js/21.287b744a.js"><link rel="prefetch" href="/docs/assets/js/22.c9506be7.js"><link rel="prefetch" href="/docs/assets/js/23.9732a229.js"><link rel="prefetch" href="/docs/assets/js/24.21ed1f05.js"><link rel="prefetch" href="/docs/assets/js/25.8f1926f6.js"><link rel="prefetch" href="/docs/assets/js/26.61b6eb9f.js"><link rel="prefetch" href="/docs/assets/js/27.3706753c.js"><link rel="prefetch" href="/docs/assets/js/28.b6b182cf.js"><link rel="prefetch" href="/docs/assets/js/29.ae979ad9.js"><link rel="prefetch" href="/docs/assets/js/3.a7f8dd77.js"><link rel="prefetch" href="/docs/assets/js/30.f2233269.js"><link rel="prefetch" href="/docs/assets/js/31.06780314.js"><link rel="prefetch" href="/docs/assets/js/32.2dad91d1.js"><link rel="prefetch" href="/docs/assets/js/33.87ec6e22.js"><link rel="prefetch" href="/docs/assets/js/34.7b75f220.js"><link rel="prefetch" href="/docs/assets/js/35.3184991f.js"><link rel="prefetch" href="/docs/assets/js/36.28248fbe.js"><link rel="prefetch" href="/docs/assets/js/37.5824a979.js"><link rel="prefetch" href="/docs/assets/js/38.99a364fe.js"><link rel="prefetch" href="/docs/assets/js/39.a5f43732.js"><link rel="prefetch" href="/docs/assets/js/4.a55d89ed.js"><link rel="prefetch" href="/docs/assets/js/40.629f78f7.js"><link rel="prefetch" href="/docs/assets/js/41.e4f58d1c.js"><link rel="prefetch" href="/docs/assets/js/42.9f41aa47.js"><link rel="prefetch" href="/docs/assets/js/43.636412b6.js"><link rel="prefetch" href="/docs/assets/js/44.3b491aef.js"><link rel="prefetch" href="/docs/assets/js/45.77df19bc.js"><link rel="prefetch" href="/docs/assets/js/46.01f53ddd.js"><link rel="prefetch" href="/docs/assets/js/47.476e85c9.js"><link rel="prefetch" href="/docs/assets/js/48.198502dc.js"><link rel="prefetch" href="/docs/assets/js/49.0d59d332.js"><link rel="prefetch" href="/docs/assets/js/5.1597c0f8.js"><link rel="prefetch" href="/docs/assets/js/50.02baf101.js"><link rel="prefetch" href="/docs/assets/js/51.c9fbd54d.js"><link rel="prefetch" href="/docs/assets/js/52.4cb4459b.js"><link rel="prefetch" href="/docs/assets/js/53.248450d7.js"><link rel="prefetch" href="/docs/assets/js/54.83f12d1e.js"><link rel="prefetch" href="/docs/assets/js/55.b18accba.js"><link rel="prefetch" href="/docs/assets/js/56.8a160b09.js"><link rel="prefetch" href="/docs/assets/js/57.b854a940.js"><link rel="prefetch" href="/docs/assets/js/58.4500f315.js"><link rel="prefetch" href="/docs/assets/js/59.59400e36.js"><link rel="prefetch" href="/docs/assets/js/6.887ba020.js"><link rel="prefetch" href="/docs/assets/js/60.4dd5b5bb.js"><link rel="prefetch" href="/docs/assets/js/61.50d8c8f6.js"><link rel="prefetch" href="/docs/assets/js/62.17d10daa.js"><link rel="prefetch" href="/docs/assets/js/63.d5f821cc.js"><link rel="prefetch" href="/docs/assets/js/64.7bf2519f.js"><link rel="prefetch" href="/docs/assets/js/65.0a1c9bc8.js"><link rel="prefetch" href="/docs/assets/js/66.1bcaa81c.js"><link rel="prefetch" href="/docs/assets/js/67.326bdf9b.js"><link rel="prefetch" href="/docs/assets/js/68.2b3b63b2.js"><link rel="prefetch" href="/docs/assets/js/69.c9b1a1a9.js"><link rel="prefetch" href="/docs/assets/js/70.a5fede78.js"><link rel="prefetch" href="/docs/assets/js/71.288d9643.js"><link rel="prefetch" href="/docs/assets/js/72.8665d6b8.js"><link rel="prefetch" href="/docs/assets/js/73.47155429.js"><link rel="prefetch" href="/docs/assets/js/74.4d4738f8.js"><link rel="prefetch" href="/docs/assets/js/75.659a325f.js"><link rel="prefetch" href="/docs/assets/js/76.a263f692.js"><link rel="prefetch" href="/docs/assets/js/77.9d86d59e.js"><link rel="prefetch" href="/docs/assets/js/78.a41b5d88.js"><link rel="prefetch" href="/docs/assets/js/79.5d0fc4cc.js"><link rel="prefetch" href="/docs/assets/js/8.80d0c81d.js"><link rel="prefetch" href="/docs/assets/js/80.01a11f6a.js"><link rel="prefetch" href="/docs/assets/js/81.9c0207d9.js"><link rel="prefetch" href="/docs/assets/js/82.12be8e04.js"><link rel="prefetch" href="/docs/assets/js/83.00fa5f46.js"><link rel="prefetch" href="/docs/assets/js/84.f14cd53c.js"><link rel="prefetch" href="/docs/assets/js/85.e9d7c0d1.js"><link rel="prefetch" href="/docs/assets/js/86.eb78fbbf.js"><link rel="prefetch" href="/docs/assets/js/87.8a31f5ae.js"><link rel="prefetch" href="/docs/assets/js/88.0f10e6dc.js"><link rel="prefetch" href="/docs/assets/js/89.a17253c9.js"><link rel="prefetch" href="/docs/assets/js/9.2fa568cb.js"><link rel="prefetch" href="/docs/assets/js/90.e56bc609.js"><link rel="prefetch" href="/docs/assets/js/91.5277db18.js"><link rel="prefetch" href="/docs/assets/js/92.b0bb8aa7.js"><link rel="prefetch" href="/docs/assets/js/93.cd790b77.js"><link rel="prefetch" href="/docs/assets/js/94.76bbe227.js"><link rel="prefetch" href="/docs/assets/js/95.4f07ca32.js"><link rel="prefetch" href="/docs/assets/js/96.2b1e8485.js"><link rel="prefetch" href="/docs/assets/js/97.e93b3461.js"><link rel="prefetch" href="/docs/assets/js/98.658063a1.js"><link rel="prefetch" href="/docs/assets/js/99.a3642e6d.js">
    <link rel="stylesheet" href="/docs/assets/css/0.styles.efa081cd.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/docs/" class="home-link router-link-active"><!----> <span class="site-name">Guru Note</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----> <svg aria-hidden="true" class="icon search-icon"><use xlink:href="#icon-search"></use></svg></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/docs/layout/" class="nav-link">
  CSS 手册
</a></div><div class="nav-item"><a href="/docs/js/" class="nav-link">
  JavaScript
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端进阶" class="dropdown-title"><span class="title">前端进阶</span> <span class="arrow down"></span></button> <button type="button" aria-label="前端进阶" class="mobile-dropdown-title"><span class="title">前端进阶</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          前端框架
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/vue/" class="nav-link router-link-active">
  Vue.js
</a></li><li class="dropdown-subitem"><a href="/docs/react/" class="nav-link">
  React.js
</a></li></ul></li><li class="dropdown-item"><h4>
          拓展语言
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/node/" class="nav-link">
  Node.js
</a></li><li class="dropdown-subitem"><a href="/docs/ts/" class="nav-link">
  TypeScript
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/docs/devbook/" class="nav-link">
  开发手册
</a></div><div class="nav-item"><a href="/docs/exam/" class="nav-link">
  金九银十
</a></div><div class="nav-item"><a href="/docs/message/" class="nav-link">
  留言板
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><div class="el-scrollbar"><div class="scrollbar-wrapper el-scrollbar__wrap el-scrollbar__wrap--hidden-default"><div class="el-scrollbar__view"><nav class="nav-links"><div class="nav-item"><a href="/docs/layout/" class="nav-link">
  CSS 手册
</a></div><div class="nav-item"><a href="/docs/js/" class="nav-link">
  JavaScript
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端进阶" class="dropdown-title"><span class="title">前端进阶</span> <span class="arrow down"></span></button> <button type="button" aria-label="前端进阶" class="mobile-dropdown-title"><span class="title">前端进阶</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          前端框架
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/vue/" class="nav-link router-link-active">
  Vue.js
</a></li><li class="dropdown-subitem"><a href="/docs/react/" class="nav-link">
  React.js
</a></li></ul></li><li class="dropdown-item"><h4>
          拓展语言
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/node/" class="nav-link">
  Node.js
</a></li><li class="dropdown-subitem"><a href="/docs/ts/" class="nav-link">
  TypeScript
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/docs/devbook/" class="nav-link">
  开发手册
</a></div><div class="nav-item"><a href="/docs/exam/" class="nav-link">
  金九银十
</a></div><div class="nav-item"><a href="/docs/message/" class="nav-link">
  留言板
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/docs/vue/" aria-current="page" class="sidebar-link">说明</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Vue 2.0 全解</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/vue/options.html" class="sidebar-link">Vue 简介 &amp; 构造选项</a></li><li><a href="/docs/vue/responsive.html" class="sidebar-link">Vue 数据响应式</a></li><li><a href="/docs/vue/computed-and-watch.html" aria-current="page" class="active sidebar-link">Vue 计算属性和监听器</a></li><li><a href="/docs/vue/template-instruction-modifier.html" class="sidebar-link">Vue 模板、指令与修饰符</a></li><li><a href="/docs/vue/advanced-options.html" class="sidebar-link">Vue 进阶属性</a></li><li><a href="/docs/vue/prop.html" class="sidebar-link">Vue Prop 自定义属性</a></li><li><a href="/docs/vue/form-and-model.html" class="sidebar-link">Vue 表单与输入绑定</a></li><li><a href="/docs/vue/router.html" class="sidebar-link">Vue Router 的原理</a></li><li><a href="/docs/vue/transition.html" class="sidebar-link">Vue 动画原理</a></li><li><a href="/docs/vue/tree-component.html" class="sidebar-link">递归组件</a></li><li><a href="/docs/vue/plugin.html" class="sidebar-link">Vue 常用插件</a></li><li><a href="/docs/vue/create-project.html" class="sidebar-link">Vue 快速搭建项目</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>VuePress</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/vue/vuepress/started.html" class="sidebar-link">快速搭建文档</a></li><li><a href="/docs/vue/vuepress/markdown.html" class="sidebar-link">Markdown 语法拓展</a></li><li><a href="/docs/vue/vuepress/element-ui.html" class="sidebar-link">添加 Element 组件</a></li><li><a href="/docs/vue/vuepress/file-path.html" class="sidebar-link">关于文件路径</a></li></ul></section></li></ul> </div></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="width:0;transform:translateX(0%);ms-transform:translateX(0%);webkit-transform:translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="height:0;transform:translateY(0%);ms-transform:translateY(0%);webkit-transform:translateY(0%);"></div></div></div></aside> <main class="page"> <div class="re-page-top"><h1 class="title"><span>Vue 计算属性和监听器</span></h1> <div class="re-page-index"><div class="page-index-title"><span class="open-catalog"><svg aria-hidden="true" class="icon arrow-right"><use xlink:href="#icon-bold-right"></use></svg> <span class="text">目录</span></span></div> <div class="page-index-content"><div class="page-catalog" style="display:none;"><div class="synopsis-wrap"><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#计算属性-computed" class="synopsis-link">计算属性（computed）</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#基础用法" class="synopsis-link">基础用法</a></li><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#添加-getter-setter" class="synopsis-link">添加 getter &amp; setter</a></li></ul></li><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#侦听器-watch" class="synopsis-link">侦听器（watch）</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#基础用法-2" class="synopsis-link">基础用法</a></li><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#对象形式" class="synopsis-link">对象形式</a></li><li class="synopsis-li"><a href="/docs/vue/computed-and-watch.html#实例调用" class="synopsis-link">实例调用</a></li></ul></li></ul></div></div></div></div></div> <div class="theme-default-content content__default" data-v-3919c100 data-v-3919c100><h2 id="计算属性-computed" data-v-3919c100><a href="#计算属性-computed" class="header-anchor" data-v-3919c100>#</a> 计算属性（computed）</h2> <h3 id="基础用法" data-v-3919c100><a href="#基础用法" class="header-anchor" data-v-3919c100>#</a> 基础用法</h3> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100>computed<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token function" data-v-3919c100>fullName</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token keyword" data-v-3919c100>return</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>firstName <span class="token operator" data-v-3919c100>+</span> <span class="token string" data-v-3919c100>' '</span> <span class="token operator" data-v-3919c100>+</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>lastName
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><h3 id="添加-getter-setter" data-v-3919c100><a href="#添加-getter-setter" class="header-anchor" data-v-3919c100>#</a> 添加 getter &amp; setter</h3> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100>computed<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  fullName<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token comment" data-v-3919c100>// getter</span>
    <span class="token function" data-v-3919c100>get</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>return</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>firstName <span class="token operator" data-v-3919c100>+</span> <span class="token string" data-v-3919c100>' '</span> <span class="token operator" data-v-3919c100>+</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>lastName
    <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
    <span class="token comment" data-v-3919c100>// setter</span>
    <span class="token function" data-v-3919c100>set</span><span class="token punctuation" data-v-3919c100>(</span>newValue<span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>var</span> names <span class="token operator" data-v-3919c100>=</span> newValue<span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>split</span><span class="token punctuation" data-v-3919c100>(</span><span class="token string" data-v-3919c100>' '</span><span class="token punctuation" data-v-3919c100>)</span>
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>firstName <span class="token operator" data-v-3919c100>=</span> names<span class="token punctuation" data-v-3919c100>[</span><span class="token number" data-v-3919c100>0</span><span class="token punctuation" data-v-3919c100>]</span>
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>lastName <span class="token operator" data-v-3919c100>=</span> names<span class="token punctuation" data-v-3919c100>[</span>names<span class="token punctuation" data-v-3919c100>.</span>length <span class="token operator" data-v-3919c100>-</span> <span class="token number" data-v-3919c100>1</span><span class="token punctuation" data-v-3919c100>]</span>
    <span class="token punctuation" data-v-3919c100>}</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><p data-v-3919c100><strong data-v-3919c100>动态改变用户信息</strong></p> <p data-v-3919c100>假如要动态显示一个用户的信息，如果用户的 nickname 不存在，就显示 email。如果 email 不存在，就显示 phone。可以用 computed 添加一个计算属性，如下：</p> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token function" data-v-3919c100>data</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token keyword" data-v-3919c100>return</span> <span class="token punctuation" data-v-3919c100>{</span>
    user<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
      nickname<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'方方'</span><span class="token punctuation" data-v-3919c100>,</span>
      email<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'123@qq.com'</span><span class="token punctuation" data-v-3919c100>,</span>
      phone<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'12345'</span>
    <span class="token punctuation" data-v-3919c100>}</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
computed<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  displayName<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token function" data-v-3919c100>get</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>const</span> user <span class="token operator" data-v-3919c100>=</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>user
      <span class="token keyword" data-v-3919c100>return</span> user<span class="token punctuation" data-v-3919c100>.</span>nickname <span class="token operator" data-v-3919c100>||</span> user<span class="token punctuation" data-v-3919c100>.</span>email <span class="token operator" data-v-3919c100>||</span> user<span class="token punctuation" data-v-3919c100>.</span>phone
    <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
    <span class="token function" data-v-3919c100>set</span><span class="token punctuation" data-v-3919c100>(</span>value<span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>user<span class="token punctuation" data-v-3919c100>.</span>nickname <span class="token operator" data-v-3919c100>=</span> value
    <span class="token punctuation" data-v-3919c100>}</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><p data-v-3919c100>这样每次修改 <code data-v-3919c100>displayName</code> 修改的都是 <code data-v-3919c100>user.nickname</code>， 而获取时是根据 <code data-v-3919c100>nickname</code>、<code data-v-3919c100>email</code>、<code data-v-3919c100>phone</code> 依次判断获取。</p> <p data-v-3919c100><strong data-v-3919c100>根据性别筛选用户列表</strong></p> <p data-v-3919c100>假设用户信息用以下方式存储</p> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token keyword" data-v-3919c100>const</span> userList <span class="token operator" data-v-3919c100>=</span> <span class="token punctuation" data-v-3919c100>[</span>
  <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>1</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'张三'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'男'</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
  <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>2</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'李四'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'女'</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
  <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>3</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'王五'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'男'</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
  <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>4</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'陈六'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'女'</span> <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>]</span>
</code></pre></div><p data-v-3919c100>使用 <code data-v-3919c100>computed</code> 来筛选其中的性别。只需要添加一个性别属性 <code data-v-3919c100>gender</code> 然后在 computed 中添加 displayList 计算属性，该属性根据 <code data-v-3919c100>gender</code> 的值改变：</p> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token function" data-v-3919c100>data</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token keyword" data-v-3919c100>return</span> <span class="token punctuation" data-v-3919c100>{</span>
    userList<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>[</span>
      <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>1</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'张三'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'男'</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
      <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>2</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'李四'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'女'</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
      <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>3</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'王五'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'男'</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
      <span class="token punctuation" data-v-3919c100>{</span> id<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>4</span><span class="token punctuation" data-v-3919c100>,</span> name<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'陈六'</span><span class="token punctuation" data-v-3919c100>,</span> gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'女'</span> <span class="token punctuation" data-v-3919c100>}</span>
    <span class="token punctuation" data-v-3919c100>]</span><span class="token punctuation" data-v-3919c100>,</span>
    gender<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>''</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
computed<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token function" data-v-3919c100>displayList</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token keyword" data-v-3919c100>const</span> hash <span class="token operator" data-v-3919c100>=</span> <span class="token punctuation" data-v-3919c100>{</span>
      male<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'男'</span><span class="token punctuation" data-v-3919c100>,</span>
      female<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>'女'</span>
    <span class="token punctuation" data-v-3919c100>}</span>
    <span class="token keyword" data-v-3919c100>const</span> <span class="token punctuation" data-v-3919c100>{</span> gender<span class="token punctuation" data-v-3919c100>,</span> userList <span class="token punctuation" data-v-3919c100>}</span> <span class="token operator" data-v-3919c100>=</span> <span class="token keyword" data-v-3919c100>this</span>
    <span class="token keyword" data-v-3919c100>if</span> <span class="token punctuation" data-v-3919c100>(</span>gender <span class="token operator" data-v-3919c100>===</span> <span class="token string" data-v-3919c100>''</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>return</span> userList
    <span class="token punctuation" data-v-3919c100>}</span> <span class="token keyword" data-v-3919c100>else</span> <span class="token keyword" data-v-3919c100>if</span> <span class="token punctuation" data-v-3919c100>(</span><span class="token keyword" data-v-3919c100>typeof</span> gender <span class="token operator" data-v-3919c100>===</span> <span class="token string" data-v-3919c100>'string'</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>return</span> userList<span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>filter</span><span class="token punctuation" data-v-3919c100>(</span><span class="token parameter" data-v-3919c100>user</span> <span class="token operator" data-v-3919c100>=&gt;</span> user<span class="token punctuation" data-v-3919c100>.</span>gender <span class="token operator" data-v-3919c100>===</span> hash<span class="token punctuation" data-v-3919c100>[</span>gender<span class="token punctuation" data-v-3919c100>]</span><span class="token punctuation" data-v-3919c100>)</span>
    <span class="token punctuation" data-v-3919c100>}</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><p data-v-3919c100>那么每次筛选时只需要改变 <code data-v-3919c100>gender</code> 的值即可：</p> <div class="language-vue extra-class" data-v-3919c100><pre class="language-vue" data-v-3919c100><code data-v-3919c100><span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>template</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
  <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>div</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@click</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>gender = <span class="token punctuation" data-v-3919c100>'</span><span class="token punctuation" data-v-3919c100>'</span><span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>全部<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@clikc</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>gender = <span class="token punctuation" data-v-3919c100>'</span>male<span class="token punctuation" data-v-3919c100>'</span><span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>男<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@clikc</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>gender = <span class="token punctuation" data-v-3919c100>'</span>female<span class="token punctuation" data-v-3919c100>'</span><span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>女<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>p</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>用户列表<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>p</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>ul</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
      <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>li</span> <span class="token attr-name" data-v-3919c100>v-for</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>(value, key) in userList<span class="token punctuation" data-v-3919c100>&quot;</span></span> <span class="token attr-name" data-v-3919c100>:key</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>key<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
        用户名：{{ value.name }}，性别：{{ value.gender }}
      <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>li</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>ul</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
  <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>div</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>template</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
</code></pre></div><p data-v-3919c100>效果如下：</p> <section class="re-part" style="min-height:222px;" data-v-3919c100><div data-v-3919c100><button data-v-3919c100>全部</button> <button data-v-3919c100>男</button> <button data-v-3919c100>女</button></div> <p data-v-3919c100>用户列表</p> <ul data-v-3919c100><li data-v-3919c100><span data-v-3919c100>姓名：张三</span>，<span data-v-3919c100>性别：男</span></li><li data-v-3919c100><span data-v-3919c100>姓名：李四</span>，<span data-v-3919c100>性别：女</span></li><li data-v-3919c100><span data-v-3919c100>姓名：王五</span>，<span data-v-3919c100>性别：男</span></li><li data-v-3919c100><span data-v-3919c100>姓名：陈六</span>，<span data-v-3919c100>性别：女</span></li></ul></section> <h2 id="侦听器-watch" data-v-3919c100><a href="#侦听器-watch" class="header-anchor" data-v-3919c100>#</a> 侦听器（watch）</h2> <p data-v-3919c100>watch 对象接受一个 函数/对象 ，且必须对应 data 中的值名称（key）。函数接受两个参数，第一个表示改变之后的值（newValue），第二个表示改变之前的值（oldValue）。示例：</p> <div class="custom-block info" data-v-3919c100><p class="custom-block-title" data-v-3919c100>watch 监听触发的条件</p><ul data-v-3919c100><li data-v-3919c100>普通类型：值改变就改变。</li> <li data-v-3919c100>对象类型：对象的地址改变触发（对象重新赋值，不论对象的内容是否相同），但是对象内对应的属性如果不变则该属性不会触发。</li></ul></div><h3 id="基础用法-2" data-v-3919c100><a href="#基础用法-2" class="header-anchor" data-v-3919c100>#</a> 基础用法</h3> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token function" data-v-3919c100>data</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token keyword" data-v-3919c100>return</span> <span class="token punctuation" data-v-3919c100>{</span>
    value<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>''</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
watch<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token function" data-v-3919c100>value</span><span class="token punctuation" data-v-3919c100>(</span><span class="token parameter" data-v-3919c100>newValue<span class="token punctuation" data-v-3919c100>,</span> oldValue</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token operator" data-v-3919c100>...</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><h3 id="对象形式" data-v-3919c100><a href="#对象形式" class="header-anchor" data-v-3919c100>#</a> 对象形式</h3> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token function" data-v-3919c100>data</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token keyword" data-v-3919c100>return</span> <span class="token punctuation" data-v-3919c100>{</span>
    value<span class="token operator" data-v-3919c100>:</span> <span class="token string" data-v-3919c100>''</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
watch<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  value<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token comment" data-v-3919c100>/* 监听值改变时执行的函数 */</span>
    <span class="token function" data-v-3919c100>handler</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span><span class="token punctuation" data-v-3919c100>{</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
    <span class="token comment" data-v-3919c100>/* 如果是对象，是否深度监听（监听对象中的每个属性），默认false */</span>
    deep<span class="token operator" data-v-3919c100>:</span> <span class="token boolean" data-v-3919c100>true</span><span class="token punctuation" data-v-3919c100>,</span>
    <span class="token comment" data-v-3919c100>/* 是否在首次赋值时触发，默认false */</span>
    immediate<span class="token operator" data-v-3919c100>:</span> <span class="token boolean" data-v-3919c100>true</span><span class="token punctuation" data-v-3919c100>,</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><h3 id="实例调用" data-v-3919c100><a href="#实例调用" class="header-anchor" data-v-3919c100>#</a> 实例调用</h3> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100>vm<span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>$watch</span><span class="token punctuation" data-v-3919c100>(</span><span class="token string" data-v-3919c100>'n'</span><span class="token punctuation" data-v-3919c100>,</span> <span class="token keyword" data-v-3919c100>function</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span><span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span> <span class="token punctuation" data-v-3919c100>{</span> immediate<span class="token operator" data-v-3919c100>:</span> <span class="token boolean" data-v-3919c100>true</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>)</span>
<span class="token comment" data-v-3919c100>/* or */</span>
<span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>$watch</span><span class="token punctuation" data-v-3919c100>(</span><span class="token string" data-v-3919c100>'n'</span><span class="token punctuation" data-v-3919c100>,</span> <span class="token keyword" data-v-3919c100>function</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span><span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span> <span class="token punctuation" data-v-3919c100>{</span> immediate<span class="token operator" data-v-3919c100>:</span> <span class="token boolean" data-v-3919c100>true</span> <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>)</span>
</code></pre></div><div class="custom-block tip" data-v-3919c100><p class="custom-block-title" data-v-3919c100>注意</p> <p data-v-3919c100>Vue 中的 watch 操作都是异步执行的，所以如果要 watch 根据当前某个值做一些后续的操作，需要调用 <a href="https://cn.vuejs.org/v2/api/#vm-nextTick" target="_blank" rel="noopener noreferrer" data-v-3919c100><code data-v-3919c100>vm.$nextTick( [callback] )</code><span data-v-3919c100><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p></div> <p data-v-3919c100><strong data-v-3919c100>模拟历史记录功能</strong></p> <p data-v-3919c100>假如要存储一个数字计算前的结果，每次值被修改时就可以利用 <code data-v-3919c100>watch</code> 存储，如下：</p> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token function" data-v-3919c100>data</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token keyword" data-v-3919c100>return</span> <span class="token punctuation" data-v-3919c100>{</span>
    number<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>0</span><span class="token punctuation" data-v-3919c100>,</span>
    history<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>[</span><span class="token punctuation" data-v-3919c100>]</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
watch<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token function" data-v-3919c100>number</span><span class="token punctuation" data-v-3919c100>(</span><span class="token parameter" data-v-3919c100>newValue<span class="token punctuation" data-v-3919c100>,</span> oldValue</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>history<span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>push</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>{</span> form<span class="token operator" data-v-3919c100>:</span> oldValue<span class="token punctuation" data-v-3919c100>,</span> to<span class="token operator" data-v-3919c100>:</span> newValue <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>)</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><p data-v-3919c100>其中 <code data-v-3919c100>history</code> 表示历史记录列表，每次 <code data-v-3919c100>number</code> 被修改，都会在 <code data-v-3919c100>history</code> 中存储一个对象，对象的 <code data-v-3919c100>form</code> 存储了改变之前的值， <code data-v-3919c100>to</code> 存储的了改变之后的值。</p> <p data-v-3919c100>这样就可以模拟计算一个具有撤回功能的数值，如下：</p> <div class="language-js extra-class" data-v-3919c100><pre class="language-js" data-v-3919c100><code data-v-3919c100><span class="token function" data-v-3919c100>data</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token keyword" data-v-3919c100>return</span> <span class="token punctuation" data-v-3919c100>{</span>
    number<span class="token operator" data-v-3919c100>:</span> <span class="token number" data-v-3919c100>0</span><span class="token punctuation" data-v-3919c100>,</span>
    history<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>[</span><span class="token punctuation" data-v-3919c100>]</span><span class="token punctuation" data-v-3919c100>,</span>
    inUndoMode<span class="token operator" data-v-3919c100>:</span> <span class="token boolean" data-v-3919c100>false</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
watch<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token function" data-v-3919c100>number</span><span class="token punctuation" data-v-3919c100>(</span><span class="token parameter" data-v-3919c100>newValue<span class="token punctuation" data-v-3919c100>,</span> oldValue</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token keyword" data-v-3919c100>if</span> <span class="token punctuation" data-v-3919c100>(</span><span class="token operator" data-v-3919c100>!</span><span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>inUndoMode<span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>history<span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>push</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>{</span> form<span class="token operator" data-v-3919c100>:</span> oldValue<span class="token punctuation" data-v-3919c100>,</span> to<span class="token operator" data-v-3919c100>:</span> newValue <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>)</span>
    <span class="token punctuation" data-v-3919c100>}</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>,</span>
methods<span class="token operator" data-v-3919c100>:</span> <span class="token punctuation" data-v-3919c100>{</span>
  <span class="token function" data-v-3919c100>clickGoBack</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
    <span class="token keyword" data-v-3919c100>const</span> length <span class="token operator" data-v-3919c100>=</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>history<span class="token punctuation" data-v-3919c100>.</span>length
    <span class="token keyword" data-v-3919c100>if</span> <span class="token punctuation" data-v-3919c100>(</span>length <span class="token operator" data-v-3919c100>&amp;&amp;</span> length <span class="token operator" data-v-3919c100>&gt;</span> <span class="token number" data-v-3919c100>0</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token punctuation" data-v-3919c100>{</span>
      <span class="token keyword" data-v-3919c100>const</span> last <span class="token operator" data-v-3919c100>=</span> <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>history<span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>pop</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span>
      <span class="token keyword" data-v-3919c100>const</span> oldValue <span class="token operator" data-v-3919c100>=</span> last<span class="token punctuation" data-v-3919c100>.</span>form
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>inUndoMode <span class="token operator" data-v-3919c100>=</span> <span class="token boolean" data-v-3919c100>true</span>
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>number <span class="token operator" data-v-3919c100>=</span> oldValue
      <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span><span class="token function" data-v-3919c100>$nextTick</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>(</span><span class="token punctuation" data-v-3919c100>)</span> <span class="token operator" data-v-3919c100>=&gt;</span> <span class="token punctuation" data-v-3919c100>{</span>
        <span class="token keyword" data-v-3919c100>this</span><span class="token punctuation" data-v-3919c100>.</span>inUndoMode <span class="token operator" data-v-3919c100>=</span> <span class="token boolean" data-v-3919c100>false</span>
      <span class="token punctuation" data-v-3919c100>}</span><span class="token punctuation" data-v-3919c100>)</span>
    <span class="token punctuation" data-v-3919c100>}</span>
  <span class="token punctuation" data-v-3919c100>}</span>
<span class="token punctuation" data-v-3919c100>}</span>
</code></pre></div><p data-v-3919c100>其中 <code data-v-3919c100>inUndoMode</code> 表示处于撤销状态，在撤销状态下禁止 <code data-v-3919c100>watch</code> 存储值，但是由于 <strong data-v-3919c100><code data-v-3919c100>watch</code> 是异步的</strong>，所以要调用 <code data-v-3919c100>vm.$nextTick</code>，等待当前代码执行完成后再关闭该状态。</p> <p data-v-3919c100>然后在 <code data-v-3919c100>template</code> 中绑定事件和值：</p> <div class="language-vue extra-class" data-v-3919c100><pre class="language-vue" data-v-3919c100><code data-v-3919c100><span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>template</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
  <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>div</span> <span class="token attr-name" data-v-3919c100>class</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>watch-demo<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token comment" data-v-3919c100>&lt;!-- 监听的数据 --&gt;</span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>div</span> <span class="token attr-name" data-v-3919c100>class</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>number<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>{{ number }}<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>div</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token comment" data-v-3919c100>&lt;!-- 操作按钮 --&gt;</span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@click</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>number += 1<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>+1<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@click</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>number -= 1<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>-1<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@click</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>number += 2<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>+2<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@click</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>number -= 2<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>-2<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token comment" data-v-3919c100>&lt;!-- 绑定撤销按钮 --&gt;</span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>div</span> <span class="token attr-name" data-v-3919c100>class</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>go-back<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
      <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>button</span> <span class="token attr-name" data-v-3919c100>@click</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>clickGoBack<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>撤销<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>button</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>div</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token comment" data-v-3919c100>&lt;!-- 展示历史数据 --&gt;</span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>div</span> <span class="token attr-name" data-v-3919c100>class</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>display-data<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
      <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;</span>pre</span> <span class="token attr-name" data-v-3919c100>class</span><span class="token attr-value" data-v-3919c100><span class="token punctuation attr-equals" data-v-3919c100>=</span><span class="token punctuation" data-v-3919c100>&quot;</span>language-js<span class="token punctuation" data-v-3919c100>&quot;</span></span><span class="token punctuation" data-v-3919c100>&gt;</span></span>{{ history }}<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>pre</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
    <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>div</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
  <span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>div</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
<span class="token tag" data-v-3919c100><span class="token tag" data-v-3919c100><span class="token punctuation" data-v-3919c100>&lt;/</span>template</span><span class="token punctuation" data-v-3919c100>&gt;</span></span>
</code></pre></div><p data-v-3919c100>效果：</p> <section class="re-part" data-v-3919c100><div class="watch1" data-v-3919c100><div class="number" data-v-3919c100>0</div> <div class="re-divider" data-v-3919c100></div> <div class="control" data-v-3919c100><button data-v-3919c100>+1</button> <button data-v-3919c100>-1</button> <button data-v-3919c100>+2</button> <button data-v-3919c100>-2</button></div> <div class="re-divider" data-v-3919c100></div> <div class="go-back" data-v-3919c100><button data-v-3919c100>撤销</button></div> <div class="re-divider" data-v-3919c100></div> <p data-v-3919c100>历史操作（history）：</p> <div class="display-data" data-v-3919c100><pre class="language-js" data-v-3919c100>[]</pre></div></div></section></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新：</span><span class="time">2021-05-28 22:06</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><svg aria-hidden="true" class="icon"><use xlink:href="#icon-left"></use></svg> <a href="/docs/vue/responsive.html" class="prev">Vue 数据响应式</a></span> <span class="next"><a href="/docs/vue/template-instruction-modifier.html">Vue 模板、指令与修饰符</a> <svg aria-hidden="true" class="icon"><use xlink:href="#icon-right"></use></svg></span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/docs/assets/js/app.cdec4db9.js" defer></script><script src="/docs/assets/js/2.993bd611.js" defer></script><script src="/docs/assets/js/14.66270d4f.js" defer></script><script src="/docs/assets/js/7.5a12abef.js" defer></script>
  </body>
</html>
